#!/bin/sh

#i2c_adres=0x44
#i2c_dev=0

i2c_adres=$(uci get tda7313.@tda7313[0].i2c_adres)
i2c_dev=$(uci get tda7313.@tda7313[0].i2c_dev)

tfiles='/tmp/hh'

mute='31'
volume='0'
bass='96' # 0x60
treble='112' # 0x70
spec_LF='128' # 0x80 # 128+0+$vol_spec
spec_RF='160' # 0xA0 # 128+32+$vol_spec
spec_LR='192' # 0xC0 # 128+64+$vol_spec
spec_RR='224' # 0xE0 # 128+96+$vol_spec

switch='64' # 0x40 # 64+0
switch1='65' # 0x41 # 64+1
switch2='66' # 0x42 # 64+2

LOUD_OFF='4'
LOUD_ON='0'
LOUD_ON0db='24'
LOUD_ON1db='16'
LOUD_ON2db='8'
LOUD_ON3db='0'

write_CMD () {
 i2cset -f -y $i2c_dev $i2c_adres $hh
}

tda7313_init () {
 echo -e volume 40'\n'subtrebl 20'\n'subbas 15'\n'bass 12'\n'treble 12'\n'input 1'\n'loundst 1'\n'mute 1 > $tfiles

 vol='40'
 var=63
 let "var -= $vol"
 hh=$(($volume + $var))
 write_CMD

 vol='20'
 var=31
 let "var -= $vol"
 hh=$(($spec_LF + $var))
 write_CMD
 hh=$(($spec_RF + $var))
 write_CMD

 vol='15'
 var=31
 let "var -= $vol"
 hh=$(($spec_LR + $var))
 write_CMD
 hh=$(($spec_RR + $var))
 write_CMD

 vol='12'
 var=23
 let "var -= $vol"
 hh=$(($bass + $var))
 write_CMD
 hh=$(($treble + $var))
 write_CMD

 vol='1' # switch1
 hh=$(($switch + $vol + $LOUD_OFF + $LOUD_ON0db))
 write_CMD
}

test_reg () {
 echo 
 cat /tmp/hh
 echo 
 echo "  reg+vol - $reg1: "$hh
}

lound_CMD () {
  case "$loundst" in	# от 1 до 5
      "1" ) lost=$LOUD_OFF; lodb=$LOUD_ON0db;;
      "2" ) lost=$LOUD_ON; lodb=$LOUD_ON0db;;
      "3" ) lost=$LOUD_ON; lodb=$LOUD_ON1db;;
      "4" ) lost=$LOUD_ON; lodb=$LOUD_ON2db;;
      "5" ) lost=$LOUD_ON; lodb=$LOUD_ON3db;;
      * ) lost=$LOUD_OFF; lodb=$LOUD_ON0db;;
  esac
}

tembr () {
    vlm=$(sed '/'$indes1' /!d' $tfiles | awk '{print $2}')
  if [[ "$indes2" == "-" && "$vlm" -eq "15" ]]; then
      let "vlm "$indes2"= 1"
  elif [[ "$vlm" -eq "0" && "$indes2" == "+" ]]; then
      let "vlm "$indes2"= 1"
  elif [[ "$vlm" -gt "0" && "$vlm" -lt "15" ]]; then
      let "vlm "$indes2"= 1"
  fi
  if [ "$vlm" -le "7" ]; then
      hh=$(($tembr + $vlm))
  else
      var=23
      let "var -= $vlm"
      hh=$(($tembr + $var))
  fi
  write_CMD
  volm=$(sed '/'$indes1' /!d' $tfiles | awk '{print $2}')
  sed -i '/'$indes1' /s/'$volm'/'$vlm'/g' $tfiles
}

balans () {
  if [[ "$indes2" -ge "0" && "$indes2" -le "31" ]]; then
    var=31
    let "var -= $indes2"
    hh=$(($balans + $var))
    write_CMD
    volm=$(sed '/'$indes1' /!d' $tfiles | awk '{print $2}')
    sed -i '/'$indes1' /s/'$volm'/'$indes2'/g' $tfiles
  fi
}

tembrbtre () {
    if [ "$tiptembr" == "sbass" ]; then
      hh=$(($spec_LR + $var))
      write_CMD
      hh=$(($spec_RR + $var))
      write_CMD
    elif [ "$tiptembr" == "streble" ]; then
      hh=$(($spec_LF + $var))
      write_CMD
      hh=$(($spec_RF + $var))
      write_CMD
    fi
}

# mute
if [ "$1" == "mute" ]; then
    bit_mute_sfile=$(sed '/mute /!d' $tfiles | awk '{print $2}')
  if [ "$bit_mute_sfile" == "1" ]; then
      hh=$mute	# 31 #
      write_CMD
      bit_mute_inwers=0
      sed -i '/mute /s/'$bit_mute_sfile'/'$bit_mute_inwers'/g' $tfiles

      bit_volm_sfile=$(sed '/volume /!d' $tfiles | awk '{print $2}')
      mutesost=$(($bit_volm_sfile - 1))
      bit_volm_sfile=$(sed '/volume /!d' $tfiles | awk '{print $2}')
      sed -i '/volume /s/'$bit_volm_sfile'/'$mutesost'/g' $tfiles
  fi

# tda7313 init
elif [[ "$1" == "tda7313_init" || "$1" == "init" ]]; then
    tda7313_init
# switch
elif [[ "$1" == "switch" || "$1" == "aux" || "$1" == "input" ]]; then
  if [[ "$2" -ge "0" && "$2" -le "3" ]]; then
    loundst=$(sed '/loundst /!d' $tfiles | awk '{print $2}')
    lound_CMD
    hh=$(($switch + $2 + $lost + $lodb))
    write_CMD
    volm=$(sed '/input /!d' $tfiles | awk '{print $2}')
    sed -i '/input /s/'$volm'/'$2'/g' $tfiles
  fi
# lound
elif [[ "$1" == "lound" ]]; then
    loundst=$(sed '/loundst /!d' $tfiles | awk '{print $2}')
  if [ "$2" == "+" ]; then
      let "loundst += 1"
      if [ "$loundst" == "6" ]; then
	loundst=1
      fi
      lound_CMD
      volm=$(sed '/input /!d' $tfiles | awk '{print $2}')
      hh=$(($switch + $volm + $lost + $lodb))
      write_CMD
      volm=$(sed '/loundst /!d' $tfiles | awk '{print $2}')
      sed -i '/loundst /s/'$volm'/'$loundst'/g' $tfiles
  fi
# volume
elif [ "$1" == "volume" ]; then
      bit_mute_sfile=$(sed '/mute /!d' $tfiles | awk '{print $2}')
  if [ "$bit_mute_sfile" == "0" ]; then
      bit_mute_inwers=1
      sed -i '/mute /s/'$bit_mute_sfile'/'$bit_mute_inwers'/g' $tfiles
  fi
  if [[ "$2" -ge "0" && "$2" -le "63" ]]; then
      var=63
      let "var -= $2"
      hh=$(($volume + $var))
      write_CMD
      volm=$(sed '/'$1' /!d' $tfiles | awk '{print $2}')
      sed -i '/'$1' /s/'$volm'/'$2'/g' $tfiles
  elif [[ "$2" == "+" || "$2" == "-" ]]; then
      vlm=$(sed '/'$1' /!d' $tfiles | awk '{print $2}')
      if [[ "$2" == "-" && "$vlm" -eq "63" ]]; then
	let "vlm "$2"= 1"
      elif [[ "$vlm" -eq "0" && "$2" == "+" ]]; then
	let "vlm "$2"= 1"
      elif [[ "$vlm" -gt "0" && "$vlm" -lt "63" ]]; then
	let "vlm "$2"= 1"
      fi
      var=63
      let "var -= $vlm"
      hh=$(($volume + $var))
      write_CMD
      volm=$(sed '/'$1' /!d' $tfiles | awk '{print $2}')
      sed -i '/'$1' /s/'$volm'/'$vlm'/g' $tfiles
  fi
# treble
elif [ "$1" == "treble" ]; then
  if [[ "$2" -ge "0" && "$2" -le "15" ]]; then
    if [ "$2" -le "7" ]; then
	hh=$(($treble + $2))
    else
	var=23
	let "var -= $2"
	hh=$(($treble + $var))
    fi
    write_CMD
    volm=$(sed '/'$1' /!d' $tfiles | awk '{print $2}')
    sed -i '/'$1' /s/'$volm'/'$2'/g' $tfiles
  elif [[ "$2" == "+" || "$2" == "-" ]]; then
    tembr=$treble; indes1=$1
	indes2=$2; tembr
  fi
# sbass - streble
elif [[ "$1" == "sbass" || "$1" == "streble" ]]; then
  if [ "$1" == "sbass" ]; then
      tmbr='subbas'
  elif [ "$1" == "streble" ]; then
      tmbr='subtrebl'
  fi
  volm=$(sed '/'$tmbr' /!d' $tfiles | awk '{print $2}')
  if [[ "$2" -ge "0" && "$2" -le "31" ]]; then
    var=31; tiptembr=$1
    let "var -= $2"; tembrbtre
    sed -i '/'$tmbr' /s/'$volm'/'$2'/g' $tfiles
  elif [[ "$2" == "+" || "$2" == "-" ]]; then
    vlm=$(sed '/'$tmbr' /!d' $tfiles | awk '{print $2}')
    if [[ "$2" == "-" && "$vlm" -eq "31" ]]; then
      let "vlm "$2"= 1"
    elif [[ "$vlm" -eq "0" && "$2" == "+" ]]; then
      let "vlm "$2"= 1"
    elif [[ "$vlm" -gt "0" && "$vlm" -lt "31" ]]; then
      let "vlm "$2"= 1"
    fi
    var=31; tiptembr=$1
    let "var -= $vlm"; tembrbtre
    sed -i '/'$tmbr' /s/'$volm'/'$vlm'/g' $tfiles
  fi
# Balance, front, rear.
elif [ "$1" == "LF" ]; then
  balans=$spec_LF; indes1=$1; indes2=$2; balans
elif [ "$1" == "RF" ]; then
  balans=$spec_RF; indes1=$1; indes2=$2; balans
elif [ "$1" == "LR" ]; then
  balans=$spec_LR; indes1=$1; indes2=$2; balans
elif [ "$1" == "RR" ]; then
  balans=$spec_RR; indes1=$1; indes2=$2; balans
# bass
elif [ "$1" == "bass" ]; then
  if [[ "$2" -ge "0" && "$2" -le "15" ]]; then
    if [ "$2" -le "7" ]; then
	hh=$(($bass + $2))
    else
	var=23
	let "var -= $2"
	hh=$(($bass + $var))
    fi
    write_CMD
    volm=$(sed '/'$1' /!d' $tfiles | awk '{print $2}')
    sed -i '/'$1' /s/'$volm'/'$2'/g' $tfiles
  elif [[ "$2" == "+" || "$2" == "-" ]]; then
    tembr=$bass; indes1=$1; indes2=$2; tembr
  fi
elif [[ "$1" == "?" || "$1" == "-?" || "$1" == "--?" || "$1" == "help" ]]; then
    echo -e '\n\t' tda7313 "init"'\t'"#" initialization'\n\n\t' tda7313 "<type>" "<value>"
    echo -e '\t' '\t' "type:"'\t'"init, volume, bass, treble, streble, sbass, switch, "
    echo -e '\t' '\t' "type:"'\t'"LF, RF, LR, RR, lound"'\n\n\t' '\t' "value:"'\t'"+, -, 0-63"'\n'
    echo -e '\t' Example:'\n\t' tda7313 "volume +"'\t'"#" Volume level "+".
    echo -e '\t' tda7313 "volume -"'\t'"#" Volume level "-"'\n\t' tda7313 "volume 55"'\t'"#" Volume level "55"
fi
 reg1=$1; test_reg
exit 0
